<?php
/*
 * Create On 2012-10-22
 * Athor  By Zhang Long
 * Email  By dwzhanglong@126.com
 */
defined('IN_YuLin') || exit('NO PERMIT!');

class DB
{
	var $host;
	var $port;
	var $user;
	var $pass;
	var $name;
	var $char;
	var $prev;	//	数据表前缀
	var $connect = false;	//	长连接
	var $db;	//	连接实例
    
	var $query;	//	当前sql操作结果集
    var $querynum;    
    var $querysql = [];
	
    function __construct($config)
    {
		$this->host = $config['host'];
		$this->port = $config['port'];
		$this->user = $config['user'];
		$this->pass = $config['pass'];
		$this->name = $config['name'];
		$this->char = $config['char'];
		$this->prev = $config['prev'];
		$this->connect = $config['connect'] ? true : false;
		
        $this->connect();
    }
	
	function __destruct()
	{
		$this->close();
	}
    
    function connect() 
    {
		try
		{
            $this->db = new PDO ('mysql:host='.$this->host.';port='.$this->port.';dbname='.$this->name,$this->user,$this->pass,[PDO::ATTR_PERSISTENT=>$this->connect]);
			$this->db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
			$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
			$this->exec ('SET NAMES '.$this->char);
        }
		catch ( PDOException $e )
		{
            $this->halt('Connect Error Infomation:'.$e->getMessage());
        }
	}
        
    function table($table) 
    {
		return $this->prev.$table;
	}
    
	//	主要用来select
    function query($sql) 
    {
        $this->query = $this->db->query ( $sql );
		
		if ($this->db->errorCode() != '00000')
		{
            $info = ($this->query) ? $this->query->errorInfo() : $this->db->errorInfo();
			$this->halt($info[2].'<br />'.$sql);			
        }
		
		$this->querysql[] = $sql;
		$this->querynum++;
		return $this->query;
	}
	
	//	主要用来update/delete/insert，返回受影响的行数
	function exec($sql)
	{
		$this->querysql[] = $sql;
		$this->querynum++;
		
		return $this->db->exec($sql);
	}
	
	function add($table,$data)
	{
		if(!$table || !$data)
		{
			return false;
		}
		$this->exec('INSERT INTO '.$this->table($table).CreateInsertSql($data));
		
		return $this->insert_id();
	}
	
	function update($table,$data,$where = '')
	{
		if(!$table || !$data)
		{
			return false;
		}
		
		$this->exec('UPDATE '.$this->table($table).' SET '.CreateUpdateSql($data).($where ? ' WHERE '.$where : ''));
		return true;
	}
    
    function getall($sql,$key = '')
    {
		$res = $this->query($sql);
		$res->setFetchMode(PDO::FETCH_ASSOC);
		$res = $res->fetchAll();
		
		if($key)
		{
			$tmp = [];
			foreach($res as $val)
			{
				$tmp[$val[$key]] = $val;
			}
			
			$res = $tmp;
		}
		
		return $res ? $res : [] ;
    }
    
    function getrow($sql)
    {
		!stripos($sql,' LIMIT ') && $sql .= ' LIMIT 1 ';
		
        $res = $this->query($sql);
		$res->setFetchMode(PDO::FETCH_ASSOC);
		$res = $res->fetch();
		
        return $res ? $res : [];
    }

	function affected_rows($sql) 
    {
		$res = $this->query($sql);
		
		return $res->rowCount();
	}

	function insert_id() 
    {
		return $this->db->lastInsertId();
	}

	function close() 
    {
		$this->db = null;
	}

	function halt($msg) 
    {
		echo '<b>MySQL Query Error:</b>'.$msg;
		exit;
	}

}

?>